<template>
    <div class="container">
        <div class="app-conyainer">
            <!-- 组织架构 -->
            <el-tree :expand-on-click-node="false" default-expand-all :data="depts" :props="defaultPorps">
                <!-- 节点结构 -->
                <template v-slot="{data}">
                    <el-row style="width :100%; height:40px"  type="flex" justify="space-between" align="middle">
                        <el-col>{{data.name}}</el-col>
                        <el-col :span="4">
                            <span class="tree-manager">{{data.managerName}}</span>
                            <el-dropdown @command="operateDept($event,data.id)">
                                <!-- 显示区域内容 -->
                                <span class="el-dropdown-link">
                                    操作<i class="el-icon-arrow-down el-icon--right" />
                                </span>
                                <!--下拉菜单选项 -->
                                <el-dropdown-menu sLot="dropdown">
                                    <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                                    <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                                    <el-dropdown-item command="del">删除</el-dropdown-item>
                                </el-dropdown-menu>
                            </el-dropdown>
                        </el-col>
                    </el-row>
                </template>
                
            </el-tree>
        </div>
        <!-- 放置弹窗 -->
        <!-- 表示会接收子组件的事件，加上点sync会监听 update:showDialog 这个事件 自动赋值 -->
        <AddDept ref="addDept" :currentNodeId="currentNodeId" :show-dialog.sync="showDialog" :updateDepartment="getDepartment"/>
    </div>
</template>
<script>
import {delDeparement,getDepartment} from "@/api/department"
import {transListToTreeData} from "@/utils"
import AddDept from './components/add-dept.vue'
export default {
    name: 'Department',
    components:{AddDept},
    data() {
        return {
            currentNodeId: null,
            showDialog: false,
            depts: [],
            defaultPorps: {
                label: "name",
                children: 'children'
            }
        }
    },
    created(){
        this.getDepartment()
    },
    methods:{
        async getDepartment(){
            const data =  await getDepartment()
            this.depts = transListToTreeData(data,0)
        },
        operateDept(type,id){
            if(type === 'add'){
                this.showDialog = true
                this.currentNodeId = id
            }else if(type === 'edit'){
                this.showDialog = true
                this.currentNodeId = id

                //调用子组件 发送请求
                this.$nextTick(()=>{
                    this.$refs.addDept.getDepartmentDetail(id)
                })
            }else{
                //删除
                this.$confirm('确认删除此部门嘛').then(async() => {
                    await delDeparement(id)
                    this.$message.success("删除成功")
                    this.getDepartment()
                })
                
            }
        }
    }
}
</script>

<style scoped>
.app-conyainer {
    padding: 30px 140px;
    font-size: 14px;
}
.tree-manager{
    width: 50px;
    display: inline-block;
    margin: 25px;
}
</style>